/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/11/23
 * Time: 13:37
 */
$(function(){
    $('.btn-save').on('click', function(){
        var params = {
            username: $('.user-name').val(),
            password: $('.user-pass').val(),
            re_password: $('.re-pass').val(),
            mobile: $('.mobile').val(),
            email: $('.email').val()
        };
        if (checkForm(params)) {
            $.ajax({
                type: 'post',
                url: '/signUp',
                data: {params: params},
                success: function(e){
                    var data = JSON.parse(e);
                    var json = data.body.data;
                    if (json) {
                        Cookies.set('_token', json._token, {expires: 1, path: '/'});
                        location.href = '/student.html';
                    }

                }
            });
        }
    });
});

function checkForm(params) {
    if (params.username == '') {
        alert('请输入用户名');
        return false;
    }
    if (params.password == '') {
        alert('请输入密码');
        return false;
    }
    if (params.re_password == '') {
        alert('请输入确认密码');
        return false;
    }
    if (params.re_password != params.password) {
        alert('两次输入密码不一致');
        return false;
    }
    if (params. mobile == '') {
        alert('请输入 手机号');
        return false;
    }
    if (params.email == '') {
        alert('请输入电子邮箱');
        return false;
    }
    var emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
    if (!emailReg.test($.trim(params.email))) {
        alert('电子邮箱格式不正确');
        return false;
    }
    return true;
}